/**
 * @(#)GenericSort.java, 2020/8/21.
 * <p/>
 * Copyright 2020 Netease, Inc. All rights reserved.
 * NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.lhh.myspringboot.Boke;

import java.util.ArrayList;

/**
 * @author lvhouhou(lvhouhou @ 163.com)
 */
public class GenericSort {

    public static void main(String[] args){

        Integer[] intArray = {new Integer(2), new Integer(4),new Integer(3)};
        Double[] doubleArray = {new Double(3.4), new Double(1.3), new Double(-22.1)};
        Character[] charArray = {new Character('a'), new Character('J'), new Character('r')};
        String[] stringArray = {"Tom", "Susan", "Kim"};

        sort(intArray);
        sort(doubleArray);
        sort(charArray);
        sort(stringArray);

        printList(intArray);
        printList(doubleArray);
        printList(charArray);
        printList(stringArray);

    }

    /* 排序  对实现Comparable接口的对象数组进行排序*/
    public static <E extends Comparable<E>> void sort(E[] list){
        E currentMin;
        int currentMinIndex;
        for (int i = 0; i < list.length - 1; i++) {
           //Find the minimum in the list[i+1...list.length-2]
            currentMin = list[i];
            currentMinIndex = i;
            for (int j = i + 1; j <list.length ; j++) {
                if (currentMin.compareTo(list[j]) > 0){
                    currentMin = list[j];
                    currentMinIndex = j;
                }

            }
            if (currentMinIndex != 1){
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
    }

    public static void printList(Object[] list){
        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i] + " ");
        }


        //原生类型：不带类型参数的泛型类
        ArrayList arrayList = new ArrayList();
        //相当于
        ArrayList<Object> objects = new ArrayList<>();
       // 原生类型是不安全的（未指定类型参数，丧失了泛型编译时检查错误的优势）,尽量不使用

    }
}
